package utils;

import java.io.*;

public class FileUtils {
    public static void main(String[] args) throws FileNotFoundException {
//        InputStream inputStream = new FileInputStream("D:\\nio_write.txt");
//        System.out.println(inputStream);
//        OutputStream outputStream = new ByteArrayOutputStream();
//        IoUtil.copy(inputStream, outputStream);
//        System.out.println(outputStream);
//
//        File file = new File("D:\\noi_write.txt");
//        System.out.println(file);
        stream();
        System.out.println("================");
        streamByBuffer();
        System.out.println("================");
        reader();
        System.out.println("================");
        readerByBuffer();
    }

    private static void stream() {
        try (InputStream is = new FileInputStream("D:\\nio_write.txt")) {
            int n;
            while ((n = is.read()) != -1) {
                System.out.print((char) n);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void streamByBuffer() {
        try (InputStream inputStream = new FileInputStream("D:\\nio_write.txt")) {
            byte[] b = new byte[1024];
            while (inputStream.read(b) != -1) {
                System.out.print(new String(b));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void reader() {
        try (Reader reader = new FileReader("D:\\nio_write.txt")) {
            int n;
            while ((n = reader.read()) != -1) {
                System.out.print((char) n);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void readerByBuffer() {
        try (Reader reader = new FileReader("D:\\nio_write.txt")) {
            char[] c = new char[1024];
            while (reader.read(c) != -1) {
                System.out.print(c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
